package com.chezai.client.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chezai.client.R;
import com.chezai.client.adapter.ListDialogAdapter;
import com.chezai.client.bean.TrainProjectEntity;

import java.util.List;

import butterknife.BindView;

/**
 * Created by likunyang on 2017/9/20.
 */

public class ListDialog extends BaseDialog implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.iv_list_dialog_close)
    ImageView ivListDialogClose;
    @BindView(R.id.flDialog)
    FrameLayout flDialog;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    private ListDialogAdapter mAdapter;
    private OnItemClickListener mListener;

    public ListDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getDialogLayoutResId() {
        return R.layout.dialog_list_dialog;
    }

    @Override
    protected void initView(Context context) {
        mAdapter = new ListDialogAdapter(R.layout.item_list_dialog);
        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        rvList.setLayoutManager(manager);
        rvList.setAdapter(mAdapter);
    }

    @Override
    protected float getwScale() {
        return 0.85f;
    }

    @Override
    protected float gethScale() {
        return 0.85f;
    }

    @Override
    protected int getGravity() {
        return 0;
    }

    @Override
    protected void setListener() {
        ivListDialogClose.setOnClickListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (mListener != null) {
            mListener.onCloseClick(this);
        }
    }

    public void setData(List<TrainProjectEntity> data) {
        if (mAdapter != null) mAdapter.setNewData(data);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (mListener != null) {
            mListener.onItemClick(this, mAdapter.getItem(position));
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Dialog dialog, TrainProjectEntity t);

        void onCloseClick(Dialog dialog);
    }
}
